﻿
#region Copyright & License
// --------------------------
// Copyright 2008 Daniel Gray
// --------------------------
// This file is part of Kale.
//
// Kale is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Kale is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Kale.  If not, see <http://www.gnu.org/licenses/>.
#endregion
#region File details
// --------------------------------------------------------
// Filename          : BaseNodeCache.cs
// Author            : Daniel Gray
// Created           : 15/04/2008
// --------------------------------------------------------
#endregion
            
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Kale.Core
{
   /// <summary>
   /// Provides a base implementation of node caching.
   /// </summary>
   public abstract class BaseNodeCache : INodeCache
   {
      #region INodeCache Members

      public abstract void AddNode(IImageNode node);
      public abstract void RemoveNode(IImageNode node);
      public abstract void RemoveNode(NodeId id);

      public void RemoveNodeAndChildren(IImageNode node)
      {
         if (HasNode(node.NodeId))
         {
            RemoveNode(node);

            foreach (IImageNode childNode in node.Children)
            {
               RemoveNodeAndChildren(childNode);
            }
         }
      }

      public void RemoveNodeAndChildren(NodeId id)
      {
         if (HasNode(id))
         {
            RemoveNodeAndChildren(this[id]);
         }
      }

      public abstract void Clear();
      public abstract bool HasNode(NodeId id);

      public IImageNode this[NodeId id]
      {
         get
         {
            return GetNode(id);
         }
      }

      public abstract IImageNode GetNode(NodeId id);

      public abstract int Count { get; }

      #endregion
   }
}
